//
//  AppDelegate.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/2/26.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        let userAccount = SWUserAccountViewModel.sharedInstance.userAccount
        print("拿到了userAccount：\(userAccount)")
        
        //设置navC的外观颜色
        setappearance()
        
        //设置窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //设置跟控制器 -- 先判断用户有没登陆
        window?.rootViewController = defaltController()
        
        //显示窗口
        window?.makeKeyAndVisible()
        
        return true
    }
    
    /**
     内部方法当前Window是否跳到Mian控制器
    - parameter isMain: true 即传Mian控制器
    */
    private func choeseRootViewController(isMain: Bool) {
        
        window?.rootViewController = isMain ? SWMainTabBarController() : SWWelcomeController()
    }
    
    /**
     外部调用是否跳转到Main控制器
     
     - parameter isMain: true: SWMainTabBarController  false:SWWelcomeController
     */
    class func outSideChoeseRootViewController(isMain: Bool) {
        
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        appDelegate.choeseRootViewController(isMain)
        
    }
    
    //设置默认控制器
    private func defaltController() ->UIViewController {
        
        if !SWUserAccountViewModel.sharedInstance.userLogin {//没登录 没登录就到main控制器现在没登录的view
            return SWMainTabBarController()
        }
        //登陆了 判断当前是否新版本 是新版本就到新特性  不是就到欢迎界面
        return isNewVersion() ? SWNewFeatureController() : SWWelcomeController()
    }
    
    //是否是最新版本
    private func isNewVersion() ->Bool {
        //取出现在版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //拿出沙盒中存的版本号
        let sandBoxVersionKey = "sandBoxVersion"
        let sanBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandBoxVersionKey)
        print("sanBoxVersion: \(sanBoxVersion)")
        if sanBoxVersion != version { //当前版本比沙盒版本大 显示新特性
            
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandBoxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
            return true
        }
        
        return false
    }
       //设置navC的外观颜色
    func setappearance() {
        let item = UIBarButtonItem.appearance()
        item.tintColor = UIColor.orangeColor()
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

